<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">

        <TreeTools :tree-data="companyInfo" :is-root="true" @add="addDepartments" />

        <hr>

        <el-tree
          :data="depts"
          :props="{label:'name'}"
          default-expand-all
          :expand-on-click-node="false"
        >
          <template #default="obj">

            <TreeTools
              :tree-data="obj.data"
              @add="addDepartments"
              @del="getDepartments"
              @edit="editDepartments"
            />

          </template>
        </el-tree>
      </el-card>

      <AddDept ref="addDept" :is-show-dialog.sync="isShowDialog" :tree-data="treeData" @add="getDepartments" />

    </div>
  </div>
</template>

<script>
import AddDept from './components/add-dept'
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/department'
import { listToTreeData } from '@/utils'
export default {
  components: {
    AddDept,
    TreeTools
  },
  data() {
    return {
      companyInfo: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''

      },
      depts: [],
      isShowDialog: false,
      treeData: {}
    }
  },
  async created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      this.depts = listToTreeData(depts, '')
    },
    addDepartments(treeData) {
      this.isShowDialog = true
      this.treeData = treeData
    },
    async editDepartments(treeData) {
      this.treeData = treeData
      await this.$refs.addDept.getDepartmentDetail(treeData.id)
      this.isShowDialog = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
